<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Noticia extends SiteController {
    
    public function __construct(){
        parent::__construct();
    }

    public function index($page = 0) {
        $this->assign('noticias', $this->get_noticias($page));
        $this->assign('total', $this->get_total_noticias());
        $this->display('noticia/index');
    }
    
    /////////////////////////////////////////
    
    private function get_noticias($page = 0){
        $offset = 0;
        if( $page != 0 ) {
            $offset = ( $page - 1 ) * LIMITE_PAGINACAO_ADMIN;
        }
        $sql = "select t from Noticias t where (t.data_fim_publicacao >= :now or t.data_fim_publicacao is null) and :now >= t.data_inicio_publicacao order by t.data_alteracao DESC";
        $now = new \DateTime();
        $now->setTime(0, 0, 0);
        $query = $this->doctrine->em->createQuery($sql);
        $query->setParameter('now', $now );
        $query->setMaxResults(LIMITE_PAGINACAO_ADMIN);
        $query->setFirstResult($offset);
        return $query->getResult();
    }
    
    private function get_total_noticias(){
        $sql = "select count(t) from Noticias t where (t.data_fim_publicacao >= :now or t.data_fim_publicacao is null) and :now >= t.data_inicio_publicacao";
        $now = new \DateTime();
        $now->setTime(0, 0, 0);
        $query = $this->doctrine->em->createQuery($sql);
        $query->setParameter('now', $now );
        return $query->getSingleScalarResult();
    }

}

/* End of file noticia.php */
/* Location: ./application/controllers/admin/noticia.php */